package com.zw.yzk.kabu.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.constant.ActivityConstant;
import com.zw.yzk.kabu.internal.component.DaggerExhibitionTicketOrderDetailsComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.TicketOrderDetailsModule;
import com.zw.yzk.kabu.management.ActivityManager;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToolbarBuilder;
import com.zw.yzk.kabu.model.TicketOrderDetailsModel;
import com.zw.yzk.kabu.presenter.TicketOrderDetailsPresenter;
import com.zw.yzk.kabu.utils.ResUtils;
import com.zw.yzk.kabu.view.view.TicketOrderDetailsView;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by zhanwei on 2017/2/9.
 */

public class TicketOrderDetailsActivity extends BaseActivity implements TicketOrderDetailsView {

    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.phone)
    TextView phone;
    @Bind(R.id.order_id)
    TextView orderId;
    @Bind(R.id.order_state)
    TextView orderState;
    @Bind(R.id.image)
    ImageView image;
    @Bind(R.id.ticket_name)
    TextView ticketName;
    @Bind(R.id.count)
    TextView count;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.goods_coast)
    TextView goodsCoast;
    @Bind(R.id.total_coast)
    TextView totalCoast;
    @Bind(R.id.layout_payment)
    View paymentLayout;

    @Inject
    TicketOrderDetailsPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_ticket_order_details;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {
        ToolbarBuilder
                .createBuilder(this)
                .setTitle(getResources().getString(R.string.goods_order_details))
                .build();
    }

    @Override
    public void initComponent() {
        DaggerExhibitionTicketOrderDetailsComponent.builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .ticketOrderDetailsModule(new TicketOrderDetailsModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityManager.getInstance().finishActivity(ConfirmExhibitionTicketActivity.class);
        String orderId = getIntent().getStringExtra(ActivityConstant.EXHIBITION_TICKET_ORDER_ID);
        getTickerOrderDetails(orderId);
        LoadingDialogManager.getInstance().showLoadingDialog(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
    }

    @Override
    public void getTicketOrderDetailsSuccess(TicketOrderDetailsModel model) {
        name.setVisibility(View.GONE);
        phone.setText(model.getPhone());
        orderId.setText(model.getId());
        orderState.setText("已支付");
        ticketName.setText(model.getExhibitionName());
        count.setText(getResources().getString(R.string.goods_count, model.getCount()));
        price.setText(ResUtils.formatCurrency(this, model.getPrice()));
        goodsCoast.setText(ResUtils.formatCurrency(this, model.getTotalPrice()));
        totalCoast.setText(ResUtils.formatCurrency(this, model.getTotalPrice()));

        Glide.with(this)
                .load(model.getImage())
                .centerCrop()
                .error(R.drawable.im_default)
                .into(image);
        paymentLayout.setVisibility(View.GONE);
    }

    @OnClick(R.id.pay_now)
    public void payNow() {
        paymentLayout.setVisibility(View.GONE);
    }

    /**
     * 获取订单详情
     *
     * @param orderId 订单号
     */
    private void getTickerOrderDetails(String orderId) {
        presenter.getTicketDetails(orderId);
    }

}
